local skynet = require "skynet"
local redis = require "skynet.db.redis"
local cjson = require "cjson"
local sharedata = require "skynet.sharedata"
local act_def   = require "act_def"

local table_unpack = table.unpack
local table_insert = table.insert
local tonumber = tonumber
local assert = assert

local events = {}
function events.on_winning(data)
    local uid = data.uid
    local game_type = data.game_type
    local table_type = data.table_type
    local winning_coins = data.coins

    R().friendsvr(1):send('.income_mgr','incr_coins',uid,winning_coins)
	R().hallsvr{key=uid}:send('.msg_handler','toagent',uid,'event_on_winning',{
        game_type = game_type,table_type = table_type,coins = winning_coins
    })
end

function events.on_play(data)
    local uid = data.uid
    local game_type = data.game_type
    local table_type = data.table_type

	R().hallsvr{key=uid}:send('.msg_handler','toagent',uid,'event_on_play',{
        game_type = game_type,table_type = table_type
    })
end

function events.on_card_type(data)
    local uid = data.uid
    local game_type = data.game_type
    local table_type = data.table_type
    local card_type = data.card_type

	R().hallsvr{key=uid}:send('.msg_handler','toagent',uid,'event_on_card_type',{
        game_type = game_type,table_type = table_type,card_type = card_type
    })
end

function events.today_online_seconds(data)
    local uid = data.uid
    local times = data.times

    R().mailsvr(1):send('.rank_handler','update_time_rank',uid,times)
end

function events.today_fech_coins(data)
    local uid = data.uid
    local coins = data.coins

    R().mailsvr(1):send('.rank_handler','update_fech_rank',uid,coins)
end

function events.today_win_coins(data)
    local uid = data.uid
    local coins = data.coins

    R().mailsvr(1):send('.rank_handler','update_coins_rank',uid,coins)
end


--玩家充值
function events.on_recharge(data, pay_coins, coins)
    local uid = data.uid
    local charge_data = {
        uid = uid, 
        pay_money = data.pay_money,
        today_pay_money = data.today_pay_money,
        charge_count = data.charge_count,
        pay_coins = pay_coins
    }
    print("_______on_recharge__",uid, charge_data)
    R().hallsvr{key=uid}:send('.msg_handler','on_recharge',charge_data)
    
    --通知首冲事件
    -- R().hallsvr({key=uid}):send('.msg_handler', 'frist_charge_rate',uid, charge_data.pay_coins, charge_data.charge_count)
    print("_______on_recharge__", data.first_charge_status)
    --充值通知到hallsvr
    if data.first_charge_status == 0 then  
        local msg = {
            uid = uid,
            coins = pay_coins,
        }
        print("_______on_recharge__msg", msg)
        R().hallsvr{key=uid}:send('.msg_handler', 'add_recharge', msg) 
    end
    print("_______on_recharge__", data.day_charge_status)
   --每日首充活动
    if data.day_charge_status == 0 then 
        local msg = {
            act_type = act_def.act_type.SHOU_CHONG,
            uid = uid,
            vip = data.vip_level,
            coins = pay_coins,
        }
        R().activitysvr(1):send('.manager','add_recharge', msg)
    end    
end

function events.on_charge_coins_reduce(charge_data)
    local uid = charge_data.uid
    R().hallsvr{key=uid}:send('.msg_handler','on_recharge',charge_data)
end

--返水奖励事件
function events.on_return_water_arward(data)
    local uid = data.uid
    --2020 3 27 屏蔽返水
   -- R().hallsvr{key=uid}:send('.msg_handler','on_return_water_arward',data)
end

function events.on_add_play_count(uid,play_count)
    -- R().activitysvr(1):send('.activity_mgr','add_act_play_count',uid,play_count)
end

function events.add_today_coins_water(uid,coins)
    print("add_today_coins_water==========",uid,coins)
    -- R().activitysvr(1):send('.activity_mgr','act_add_today_coins',uid,coins)
end


--个人流水增加
function events.add_person_water(uid, data, water_value)

    --首充
    if data.first_charge_status == 1 then --已达首充条件
        --通知首充反利活动
        if not data.person_water2 then 
            data.person_water2 = 0
        end
        local old_water = data.person_water2
        data.person_water2 = data.person_water2 + water_value
        local msg = {
            uid = uid,
            -- act_type = act_def.act_type.SHOU_CHONG,
            person_water = data.person_water2,
            old_water = old_water, --未增加前的流水
        }                
        dbglog("________add_person_water_______", uid, msg)
        -- R().activitysvr(1):send('.manager','add_person_water', msg) 
        R().hallsvr{key=uid}:send('.msg_handler','add_shouchong_water', msg) 
    end

    --通知大厅
    R().hallsvr{key=uid}:send('.msg_handler','add_person_water',{uid = uid, water_value = water_value,vip_level = data.vip_level})

end

--救援金事件
function events.on_rescue_win_lose(data)
    data.act_type = act_def.act_type.RESCUE
    dbglog("on_rescue_win_lose",data)
    R().activitysvr(1):send('.manager','on_rescue_win_lose', data)
end

return events